package com.vp.org.dao.impl;

import com.vp.org.constant.Sql;
import com.vp.org.dao.BookInfoDao;
import com.vp.org.pojo.AdminInfo;
import com.vp.org.pojo.BookInfo;
import com.vp.org.pojo.UserInfo;
import com.vp.org.util.JdbcUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.awt.print.Book;
import java.util.List;
import java.util.Map;

public class BookInfoDaoImpl extends JdbcUtil implements Sql, BookInfoDao {

    // 查询书籍 queryAllBooks
    @Override
    public List<BookInfo> queryAllBooks() {
        return template.query(BOOKINFO_QUERYALLBOOKS, new BeanPropertyRowMapper<>(BookInfo.class));
    }

    //查询书籍详情 queryBookDetails
    // 书籍默认显示（按时间倒序显示,查询所有）(可以根据实际情况，控制每页显示的条数)
    // select * from book_info where id = 2;
    @Override
    public BookInfo queryBookDetails(BookInfo bookInfo) {
        return template.queryForObject(BOOKINFO_QUERYBOOKDETAILS, new BeanPropertyRowMapper<>(BookInfo.class), bookInfo.getId());
    }

    // 模糊查询书籍 fuzzy query book（加分页）
    // select * from book_info where name like '%读%' limit 2,9;
    @Override
    public List<BookInfo> fuzzyQueryBook(int page, BookInfo bookInfo) {
        return template.query(BOOKINFO_FUZZYQUERYBOOK,new BeanPropertyRowMapper<>(BookInfo.class),"%" + bookInfo.getName() + "%",(page-1)*9);
    }

    // 模糊查询书籍页数 fuzzy query book page
    // select count(*) count from book_info where name like '%读%';
    @Override
    public long fuzzyQueryBookPage(BookInfo bookInfo) {
        return (long) template.queryForMap(BOOKINFO_FUZZYQUERYBOOKPAGE,"%" + bookInfo.getName() + "%").get("count");
    }


    // 模糊查询书籍是否存在 fuzzy query book is exist
    @Override
    public boolean fuzzyQueryBookIsExist(BookInfo bookInfo) {
        return fuzzyQueryBookPage(bookInfo) != 0;
    }

    // 书籍分页查询 book paging query
    // select * from book_info where  grade = 16 limit 0,3;
    @Override
    public List<BookInfo> bookPagingQuery(BookInfo bookInfo, int page, int count) {
        return template.query(BOOKINFO_BOOKPAGINGQUERY, new BeanPropertyRowMapper<>(BookInfo.class), bookInfo.getGrade(), (page - 1) * count, count);
    }

    // 分页展示页数 paging show page
    // select count(*) from book_info where  grade = 16;
    @Override
    public long pagingShowPage(BookInfo bookInfo) {
        return (long) template.queryForMap(BOOKINFO_PAGINGSHOWPAGE,bookInfo.getGrade()).get("count");

    }

    // 后台添加书籍 adminAddBook
    // insert into book_info values(default,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);
    @Override
    public int adminAddBook(BookInfo bookInfo, AdminInfo adminInfo) {

        return template.update(BOOKINFO_ADMINADDBOOK,bookInfo.getProductNum(),bookInfo.getName(),
                bookInfo.getPrice(),bookInfo.getDiscountPrice(),bookInfo.getImg(),bookInfo.getDetails(),
                bookInfo.getDiscount(), bookInfo.getStatus(),bookInfo.getType(),bookInfo.getDeadline(),
                bookInfo.getFitSpecialty(), bookInfo.getFitGrade(),bookInfo.getAuthor(),bookInfo.getPublishHouse(),
                bookInfo.getPublishDate(), bookInfo.getIsbn(),bookInfo.getGrade(),bookInfo.getIntegral(),
                adminInfo.getAdmin(),bookInfo.getUploadTime(),bookInfo.getBigImg(),bookInfo.getExchange());
    }

    // 后台查询书籍 adminQueryBook
    // select * from book_info where admin = 'admin' limit 0,6;
    @Override
    public List<BookInfo> adminQueryBook(AdminInfo adminInfo, int page) {
        return template.query(BOOKINFO_ADMINQUERYBOOK,new BeanPropertyRowMapper<>(BookInfo.class),adminInfo.getAdmin(),(page-1)*6);

    }

    // 后台查询书的总数
    @Override
    public long BookPage() {
        return (long) template.queryForMap(BOOKINFO_ADMINQUERYBOOKPAGE).get("count");
    }

    // 后台查询书籍页数 adminQueryBookPage
    // select count(*) count from book_info where admin = 'admin';
    @Override
    public int adminQueryBookPage() {
        int totalSize = (int) BookPage();
        return totalSize % 7 == 0 ? totalSize / 7 : totalSize / 7 + 1;
    }

  // 书后台籍分页查询 adminBookPagingQuery
    @Override
    public List<BookInfo> adminBookPagingQuery(int page, int count) {
        return template.query(BOOKINFO_ADMINBOOKPAGINGQUERY, new BeanPropertyRowMapper<>(BookInfo.class), (page - 1) * count, count);
    }

    // 后台书籍删除
    // delete from book_info where id = 36;
    @Override
    public int adminDeleteBook(BookInfo bookInfo, AdminInfo adminInfo) {
        return template.update(BOOKINFO_ADMINDELETEBOOK,bookInfo.getId());
    }

    // 后台书籍修改
    @Override
    public int adminUpdateBook(BookInfo bookInfo ) {
        return template.update(BOOKINFO_ADMINUPADTEBOOK,bookInfo.getProductNum(),bookInfo.getName(),
                bookInfo.getPrice(),bookInfo.getDiscountPrice(),bookInfo.getImg(),
                bookInfo.getDiscount(), bookInfo.getStatus(),bookInfo.getType(),
                bookInfo.getFitGrade(),bookInfo.getAuthor(),bookInfo.getPublishHouse(),
                bookInfo.getPublishDate(),bookInfo.getIntegral(), bookInfo.getUploadTime(),bookInfo.getId());
    }

    //促销商品总数
    // select count(discount) from book_info where discount < 1;
    @Override
    public long promotionCount() {
        return (long) template.queryForMap(BOOKINFO_PROMOTIONCOUNT).get("count");
    }

    // 促销商品总页数
    @Override
    public int promotionPage() {
        int count = (int) promotionCount();
        return count % 6 == 0 ? count / 6 : count / 6 + 1;
    }

    // 促销商品分页查询
    // select id,product_num,name,price,discount_price,img,discount,status,type,fit_grade,author,publish_house,publish_date,integral,upload_time from book_info where discount < 1 limit 0,6;
    @Override
    public List<BookInfo> promotionBooks(int page) {
        return template.query(BOOKINFO_PROMOTIONBOOKS,new BeanPropertyRowMapper<>(BookInfo.class),(page - 1)*6);
    }


}
